package com.tuozixuan.reentrantlock;

import java.util.concurrent.locks.ReentrantLock;

public class ReentrantLockDemo extends Thread {

	private ReentrantLock lock = new ReentrantLock();

	private static int i = 0;

	public ReentrantLockDemo(String name) {
		super.setName(name);
	}

	@Override
	public void run() {
		for (int j = 0; j < 100000; j++) {
			lock.lock();
			try {
				System.out.println(this.getName() + " " + i);
				i++;
			} finally {
				lock.unlock();
			}
		}
	}

	public static void main(String[] args) throws InterruptedException {
		ReentrantLockDemo test1 = new ReentrantLockDemo("thread1");
		ReentrantLockDemo test2 = new ReentrantLockDemo("thread2");
		ReentrantLockDemo test3 = new ReentrantLockDemo("thread3");

		test1.start();
		test2.start();
		test3.start();
		test1.join();
		test2.join();
		test3.join();
		System.out.println(i);
	}

}
